Type.registerNamespace("AjaxLab.Util");

AjaxLab.Util.CmdUtil = function(CommandPromptContainer, CommandPromptTextArea, DebugArea, CommandPromptRoot)
{
			var promptSeparator						=	":\\>";
			var commandpromptID                     =   "CommandPrompt";
            var commandPrompt                       =    $(commandpromptID);
			var copyright                           =   "AjaxLab Web Command Prompt 1.0 (LGPL) \n" +
														 "<C> CopyRighted 2006 AjaxLab.com \n"+
														 "Window frame provided by Prototype Window class @ \n"+
														 "http://prototype-window.xilinus.com/ \n"+
														 "\n";
			var root								=	"/";
			
			this.Log4J								=	new AjaxLab.Log4J(DebugArea);
			var isDebug                             =   false;
			var debug;
			var debug_area							=	"debugZone";
			var instance = this;
			
			
			function Trim_Trailing_LineFeed_Helper(s)
            {
                if(s!=null)
                {
                    while( (s.charCodeAt(s.length-1) == 10) || (s.charCodeAt(s.length-1) == 13) )
                    {
                        s = s.substring(0,s.length-1);
                    }
                }
                return s;
            }

            function Trim_Trailing_LineFeed(element){
                var s = element.value;  
                s = Trim_Trailing_LineFeed_Helper(s);
                element.value=s;
            }
            
			function scrollElementToEnd(element){
                if (typeof element.scrollTop != 'undefined' &&
                   typeof element.scrollHeight != 'undefined') {
                 element.scrollTop = element.scrollHeight;
               }
            }
	
	    	this.rePosition			=	function()
			{
                var content = instance.getCommandPrompt();
                Trim_Trailing_LineFeed(content);
                scrollElementToEnd(content);
            }
            
			this.getLog4J		= 	function()
			{
				return instance.Log4J;
			}
			
			
			this.getPromptSeparator = function()
			{
				return promptSeparator;
			}
			
			this.getCopyRight = function()
			{
				return copyright;
			}
			
			this.setIsDebug = function (isdebugParam)
			{
				if(this.Log4J!=null)
				{
					this.Log4J.setIsDebug(isdebugParam);
				}
				
			}
			
			this.getIsDebug = function()
			{
				return isDebug;
			}
			
			this.setDebugZone								=	function(debugZone)
			{
				debug_area		=	debugZone;
			}
			
			this.getDebugZone								=	function()
			{
				return $(debug_area);
			}
			
			this.setCommandPromptID                         =    function(commandPromptIDParam)
            {
                commandpromptID =   commandPromptIDParam;
            }
    
            this.getCommandPromptID                         =    function()
            {
                return commandpromptID;
            }

			this.setCommandPrompt                         =    function(commandPromptIDParam)
            {
                commandpromptID =   commandPromptIDParam;
            }
			
            this.getCommandPrompt                           =    function()
            {
                return $(commandpromptID);
            }

            this.setCopyright                               =    function(copyrightParam)
            {
                copyright = copyrightParam;
            }
            this.getCopyright                               =    function(){
                return copyright;
            }

            this.setRoot                                    =    function(rootParam){
                root    =   rootParam;
            }

            this.getRoot                                    =    function(){
                return root;
            }	
			
			instance.onPreLoad(CommandPromptContainer, CommandPromptTextArea, DebugArea, CommandPromptRoot);
}

AjaxLab.Util.CmdUtil.registerClass('AjaxLab.Util.CmdUtil', AjaxLab.Util.CmdUtil);

AjaxLab.Util.CmdUtil.prototype = {

		onPreLoad							:	function(CommandPromptContainer, CommandPromptTextArea, DebugArea, CommandPromptRoot)
		{
				/* Below methods will be transftered to CmdUtil*/
				this.setCommandPrompt(CommandPromptTextArea);
				this.setDebugZone(DebugArea);
				this.setRoot(CommandPromptRoot);
				this.clearScreen();
		},
		
		emptyScreen					:	 function()
		{
				this.getCommandPrompt().value = "";
		},
		
		clearScreen					: 	 function()
		{
				this.emptyScreen();
				this.print(this.getCopyright());
                this.println();
                this.print(this.getRoot());
                this.print(this.getPromptSeparator());
		},
		
		emptyDebugZone				:	function()
		{
				this.getDebugZone().innerHTML = "";
				this.println();
				this.print(this.getRoot());
				this.print(this.getPromptSeparator());
		},
		
	  	appendToConsole:            function (myValue){
            
            var myField         =   this.getCommandPrompt();
            if(myField!=null)
            {
                if (document.selection) {
                    myField.focus();
                    sel = document.selection.createRange();
                    sel.text = myValue;
                }
                else if (myField.selectionStart || myField.selectionStart == '0') {
                    var startPos = myField.selectionStart;
                    var endPos = myField.selectionEnd;
                    myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
                } else {
                    myField.value += myValue;
                }
            }
        },  
           
        cd    :                     function(valueParam)
        {
            this.setRoot(valueParam);
			this.newPrompt();
        },
 
        print :                     function(valueParam)
		{
            this.appendToConsole(valueParam);
        }, 

		println:					function(valueParam)
		{
			var value = "";
			if(valueParam!=null)
			{
				value = valueParam;
			}
			value = value+"\n";
			this.appendToConsole(value);
		}, 
	
		printToConsole:             function(valueParam){
			this.println();
            this.println(valueParam);
			this.println();
            this.print(this.getRoot()+this.getPromptSeparator());
        },
		
		newPrompt:					function()
		{
				var text = this.getCommandPrompt().value;
				
				text = text + "\n"+this.getRoot()+":\\>";
				this.getCommandPrompt().value = text;
				this.charactercount=0;
				this.rePosition();
		}

}
